package tinybase.pf;

import tinybase.basic.RedBase;
import tinybase.basic.RC;

public class PF_Errors {
	static String[] PF_WarnMsg = {
		  "page pinned in buffer",
		  "page is not in the buffer",
		  "invalid page number",
		  "file open",
		  "invalid file descriptor (file closed)",
		  "page already free",
		  "page already unpinned",
		  "end of file",
		  "attempting to resize the buffer too small",
		  "invalid filename"
		};

		static String[] PF_ErrorMsg = {
		  "no memory",
		  "no buffer space",
		  "incomplete read of page from file",
		  "incomplete write of page to file",
		  "incomplete read of header from file",
		  "incomplete write of header from file",
		  "new page to be allocated already in buffer",
		  "hash table entry not found",
		  "page already in hash table",
		  "invalid file name"
		};

	public static void printErrors(RC rc) {
		// Check the return code is within proper limits
		if ((rc.getValue() >= RedBase.START_PF_WARN) &&
			rc.getValue() <= RC.PF_LASTWARN.getValue())
			// Print warning
			System.err.println("PF warning: " + PF_WarnMsg[rc.getValue() - 
			   RedBase.START_PF_WARN]);
		// Error codes are negative, so invert everything
		else if ((0-rc.getValue()) >= (0 - RedBase.START_PF_ERR) &&
			(0-rc.getValue()) <= (0-RC.PF_LASTERR.getValue())) {
			// Print error
//			System.err.println("PF error: " + PF_ErrorMsg[0-rc.getValue() + RedBase.START_PF_ERR]);
			System.err.println("PF error: " + rc.toString());
		}
		else if (rc == RC.PF_UNIX) 
			System.err.println("OS errors.");
		else if (rc.getValue() == 0)
			System.err.println("PF_PrintError called with return code of 0");
		else
			System.err.println("PF error: " + rc.getValue() 
					+ " is out of bounds.");
	}
}
